package com.lin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lin.entity.GroupInformation;
import com.lin.entity.ProjectInviteTalents;
import com.lin.entity.TalentJoin;
import com.lin.mapper.GroupInformationMapper;
import com.lin.mapper.ProjectInviteTalentsMapper;
import com.lin.security.util.SecurityUtil;
import com.lin.service.ProjectInviteTalentsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 林伟鸿
 * @since 2022-04-17
 */
@Service
@SuppressWarnings({"all"})
public class ProjectInviteTalentsServiceImpl extends ServiceImpl<ProjectInviteTalentsMapper, ProjectInviteTalents> implements ProjectInviteTalentsService {

    @Resource
    GroupInformationMapper groupInformationMapper;

    @Override
    public List<ProjectInviteTalents> getInvitesList() {
        int currentUserUid = SecurityUtil.getCurrentUserUid();
        List<ProjectInviteTalents> inviteTalentsList =
                this.list(new QueryWrapper<ProjectInviteTalents>().eq("user_id", currentUserUid));
        return inviteTalentsList;
    }

    @Override
    public void inviteProcess(ProjectInviteTalents projectInviteTalents) {
        int currentUserUid = SecurityUtil.getCurrentUserUid();
        ProjectInviteTalents projectInviteTalentsDB = this.getById(projectInviteTalents);
        projectInviteTalentsDB.setType(projectInviteTalents.getType());
        this.updateById(projectInviteTalentsDB);
        if (projectInviteTalentsDB.getType() == 0) {//同意邀请
            Integer groupId = projectInviteTalentsDB.getGroupId();
            GroupInformation groupInformation = groupInformationMapper.selectOne(new QueryWrapper<GroupInformation>()
                    .eq("group_id", groupId));
            Integer masterId = groupInformation.getMasterId();
            Integer memberOneId = groupInformation.getMemberOneId();
            Integer memberTwoId = groupInformation.getMemberTwoId();
            Integer memberThreeId = groupInformation.getMemberThreeId();
            Integer memberFourId = groupInformation.getMemberFourId();
            if ((masterId.equals(currentUserUid)) || (memberOneId != null && memberOneId.equals(currentUserUid))
                    || (memberTwoId != null && memberTwoId.equals(currentUserUid))
                    || (memberThreeId != null && memberThreeId.equals(currentUserUid))
                    || (memberFourId != null) && memberFourId.equals(currentUserUid)) {
                throw new RuntimeException("您已经在团队当中！");
            } else {
                if (memberOneId == null) {
                    groupInformation.setMemberOneId(projectInviteTalentsDB.getUserId());
                }else if (memberTwoId == null) {
                    groupInformation.setMemberTwoId(projectInviteTalentsDB.getUserId());
                }else if (memberThreeId == null) {
                    groupInformation.setMemberThreeId(projectInviteTalentsDB.getUserId());
                }else if (memberFourId == null) {
                    groupInformation.setMemberFourId(projectInviteTalentsDB.getUserId());
                }else {
                    throw new RuntimeException("团队已经满员！");
                }
                groupInformationMapper.updateById(groupInformation);
            }
        }
    }
}
